package com.zhuchaooasystem.adapter.holder;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.entity.ServiceImageEntity;
import com.zhuchaooasystem.ui.common.ImagePagerActivity;
import com.zhuchaooasystem.ui.home.service.ServiceImageActivity;
import com.zhuchaooasystem.utils.AppUtils;
import com.zhuchaooasystem.utils.ScreenUtils;

/**
 * @author yinsi
 */
public class ServiceImageHolder extends BaseViewHolder<ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean> {
    //----
    private RelativeLayout rootView;
    private ImageView iv_cover;
    private RelativeLayout relative_checkbox;
    private ImageView iv_check;

    //----
    private ServiceImageActivity activity;
    private Context mContext;
    private int image_height;

    public ServiceImageHolder(ViewGroup parent, ServiceImageActivity activity) {
        super(parent, R.layout.item_activity_service_image);
        this.activity = activity;
        mContext = getContext();
        //----
        rootView = $(R.id.item_act_service_image_relative_rootview);
        iv_cover = $(R.id.item_act_service_image_iv_cover);
        relative_checkbox = $(R.id.item_act_service_image_relative_checkbox);
        iv_check = $(R.id.item_act_service_image_iv_check);
        //----
        image_height = (ScreenUtils.getScreenWidth((Activity) mContext) - ScreenUtils.dip2px(mContext, 20)) / activity.getNumColomn();
        iv_cover.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, image_height));
    }

    @Override
    public void setData(final ServiceImageEntity.ResultBean.SuccessfulBean.PathContentBean pathContentBean) {
        //解决上下滑动checkbox错乱问题
        iv_check.setTag(getDataPosition());
        //----
        Glide.with(mContext).load(pathContentBean.getFilePath())
                .asBitmap().error(R.drawable.ic_image_default).diskCacheStrategy(DiskCacheStrategy.NONE).centerCrop().into(iv_cover);
        //----
        rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putStringArrayList("images", activity.getImageList());
                bundle.putBoolean("isCache", false);
                bundle.putInt("position", getDataPosition());
                AppUtils.jump(mContext, ImagePagerActivity.class, bundle);
            }
        });
        //----
        if (activity.isShowCheck()){
            relative_checkbox.setVisibility(View.VISIBLE);
            //全选状态
            if(activity.isSelectAll()){
                iv_check.setImageResource(R.drawable.ic_oval_checked);
            }else{
                if (null != activity.getCheckPositionlist()) {
                    if (activity.getCheckPositionlist().contains(getDataPosition())) {
                        iv_check.setImageResource(R.drawable.ic_oval_checked);
                    } else {
                        iv_check.setImageResource(R.drawable.ic_oval_uncheck);
                    }
                }
            }
            relative_checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //设置图片路径选中不选中的添加或移除
                    if (activity.getImageChoosedList().contains(pathContentBean.getFilePath())) {
                        activity.getImageChoosedList().remove(activity.getImageChoosedList().indexOf(pathContentBean.getFilePath()));
                        activity.getCheckPositionlist().remove(activity.getCheckPositionlist().indexOf(iv_check.getTag()));
                        iv_check.setImageResource(R.drawable.ic_oval_uncheck);
                    } else {
                        activity.getImageChoosedList().add(pathContentBean.getFilePath());
                        activity.getCheckPositionlist().add((Integer) iv_check.getTag());
                        iv_check.setImageResource(R.drawable.ic_oval_checked);
                    }
                    //设置确定按钮
                    if (null != activity.getImageChoosedList() && activity.getImageChoosedList().size() > 0) {
                        activity.setTxtDesc(activity.getImageChoosedList().size());
                    } else {
                        activity.setTxtDesc(0);
                    }
                }
            });
        }else{
            relative_checkbox.setVisibility(View.GONE);
        }

    }

}
